package com.aote.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.aote.dao.PermissionDao;
import com.aote.dao.RoleDao;
import com.aote.dao.UserDao;
import com.aote.pojo.Permission;
import com.aote.pojo.Role;
import com.aote.pojo.User;
import com.aote.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

//    根据用户名查询数据库获取用户信息和关联的角色信息，同时查询角色和关联的权限信息
    public User findByUsername(String username) {
        if (username==null){
            return null;
        }
        User user = userDao.findByUsername(username);  //查询用户基本信息  不包含角色
        if (user==null){
            return null;
        }
        Integer id = user.getId();
//        根据id查询角色
        Set<Role> roles = roleDao.findByUserId(id);
        for (Role role : roles) {
//            根据角色查询关联的权限
            Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色
        return user;
    }
}
